package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Custurn;
import cn.wolfcode.crm.mapper.CusturnMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.impl.CusturnQuery;
import cn.wolfcode.crm.service.ICusturnService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CusturnServiceImpl implements ICusturnService {
    @Autowired
    CusturnMapper custurnMapper;
    @Autowired
    EmployeeMapper employeeMapper;

    public void save(Custurn record) {
        if (record.getOldinchargeuser()!= null) {
            if (record.getOldinchargeuser().getUsername() != null) {
                Long id = employeeMapper.getByUsername(record.getOldinchargeuser().getUsername()).getId();
                record.getOldinchargeuser().setId(id);

            }
        }
        record.setId(null);
        record.setTurndate(new Date());
        custurnMapper.insert(record);
    }

    public Custurn get(Long id) {
        return custurnMapper.selectByPrimaryKey(id);
    }


    public PageResult query(CusturnQuery qo) {
        int rows = custurnMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<Custurn> result = custurnMapper.queryForList(qo);
        return new PageResult(rows, result);
    }

    public List<Custurn> selectAll() {
        return custurnMapper.selectAll();

    }
}
